app.controller('fileUploadCtrlTest', ['$element', '$scope', 'FileUploader', 'uploadAddress', 'signature', function($element, $scope, FileUploader, uploadAddress, signature) {
    $scope.$on('ngDialog.opened', function(e, $dialog) {
        $dialog.find(".ngdialog-message").height($dialog.find(".ngdialog-content").height() - 40)
    });
    var uploader = $scope.uploader = new FileUploader({
        url: uploadAddress
    });
   $scope.column2 = 0;
   $scope.imgNew=[
        {
            text:'卧室图片',
            name: 'bedRoom',
            value: []
        },
        {
            text:'客厅图片',
            name: 'livingRoom',
            value: []
        },
        {
            text:'卫生间图片',
            name: 'bathRoom',
            value: []
        },
        {
            text:'厨房图片',
            name: 'kitchenRoom',
            value: []
        },
        {
            text:'周边环境',
            name: 'environment',
            value: []
        }
   ];
    // FILTERS

    uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/ , options) {
            return this.queue.length < 10;
        }
    });

    // CALLBACKS

    uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/ , filter, options) {
        //console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
        //console.info('onAfterAddingFile', fileItem);
    };
    uploader.onAfterAddingAll = function(addedFileItems) {
        //console.info('onAfterAddingAll', addedFileItems);
    };
    uploader.onBeforeUploadItem = function(item) {
        return item.formData = [signature.run('9999', {})];
       // console.info('onBeforeUploadItem', item);
    };
    uploader.onProgressItem = function(fileItem, progress) {
        //console.info('onProgressItem', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        //console.info('onProgressAll', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        //console.info('onSuccessItem', fileItem, response, status, headers);
        // console.info('uploader', uploader);
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        //console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
        //console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        console.info('onCompleteItem', fileItem, response, status, headers);
        if($scope.indexSelect != -1){
            $scope.imgNew[$scope.typeSelect].value.splice($scope.indexSelect, 1, response.res.data.imgList[0] );
        }else{
            $scope.imgNew[$scope.typeSelect].value.push (response.res.data.imgList[0]) ;
        }
        $('#uploaderFileInput').val('');
    };
    uploader.onCompleteAll = function() {
    };

    $scope.makeSureConfirm = function() {
        $scope.confirm($scope.uploader);
    }
    $scope.remove = function(index,indexParent) {
			$scope.imgNew[$scope.typeSelect].value.splice(index, 1);
	}
    $scope.typeSelect = '';
   $scope.indexSelect = '';
    $scope.add = function(index) {
            $scope.typeSelect = index;
            $scope.indexSelect = -1;
			$('#uploaderFileInput').click();
	}
    $scope.edit = function(index,indexParent) {
            $scope.typeSelect = indexParent;
             $scope.indexSelect = index;
			$('#uploaderFileInput').click();
	}
    $scope.readFile = function(){
            setTimeout("$('#itemUpload').click();",20);
    }
}]);
